package com.zhuss.core.processor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.login.wechat.SendCodeBo;
import com.zhuss.bean.unit.core.TemplateParam;
import com.zhuss.bean.vo.core.login.SendCodeVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.exception.WpsException;
import com.zhuss.util.AliSmsService;
import com.zhuss.util.JsonUtil;

/**
 * 验证码 processor
 * @Author: zhushanshuo
 * @Date: 2019/12/24 17:55
 */
@Component("codeSend")
public class CodeSend extends AbstractProcessor<SendCodeVo, SendCodeBo> implements Processor<SendCodeVo, SendCodeBo> {
	
	@Autowired
	private AliSmsService aliSmsService;
	
    /**
     * 校验
     */
    @Override
    public void check(SendCodeBo bo) throws WpsException {
    	String username = bo.getUsername();
    	if (username == null || "".equals(username.trim())) {
    		throw new WpsException(ExceptionCode.手机号为空);
    	}
    }
    
    public static void main(String[] args) {
		
	}
    
    /**
     * 操作方法
     * @throws WpsException 
     */
	@Override
    public SendCodeVo process(SendCodeBo bo) throws WpsException {
		Double random = Math.random() * 1000000d;
		Integer code = random.intValue();
		TemplateParam param = new TemplateParam();
		param.setCode(code);
		aliSmsService.sendLoginMsg(bo.getUsername(), JsonUtil.toJson(param));
		SendCodeVo result = new SendCodeVo();
		result.setUsername(bo.getUsername());
		result.setCode(code);
        return result;
    }
    
    /**
     * 操作日志
     */
	@Override
	public SendCodeVo log(SendCodeBo param) throws WpsException {
		/*OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.验证码.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName("用户ID:" + param.getTargetId());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getTargetId());
    	record.setCreator("用户ID:" + param.getTargetId());
    	record.setCreateTime(new Date());
    	log(record);*/
    	return null;
	}
}
